package web.jdbc;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

import web.clazz.Clazz;
@Deprecated
public class ParseSQL<T> {
	/**
	 * 拼装SQL
	 * @param t 传入目标实体
	 * @param useKey 是否使用主键
	 * @param sqlType SQL类别： 增、删、改、查
	 * @param Sets Clazz集合
	 * @return sql SQL语句
	 */
	public String parseSQL(T t, boolean useKey, String sqlType,Set<Clazz> Sets) {
		String sql = "";
		Clazz clazz = null;
		for (Clazz c : Sets) {// 从扫描信息中获取目标实体对象
			if (c.getClazz().getName().equals(t.getClass().getName())) {
				clazz = c;
				break;
			}
		}

		Map<Field, Method> map = FieldsGetSet.getFieldGetOrSet(t.getClass(), "get");
		String key = clazz.getKey().keySet().iterator().next();// 获取主键变量名
		Object result = null;// 记录执行get方法的返回结果
		Object keyValue = new Object();

		String names = "";
		String values ="";
		try {
			for (Field field : map.keySet()) {//获取主键对应的实体属性值
				if (field.getName().equals(key)) {
					result = map.get(field).invoke(t);
					keyValue = result;
					break;
				}
			}
			if (result != null && !"".equals(result)) {
				switch (sqlType) {
				case "SELECT":
					sql += sqlType + " * FROM " + clazz.getName() + " WHERE " + key + "='" + keyValue + "'";// 拼装主键不考虑int类型的''符号
					break;
					
				case "INSERT":
					sql += sqlType + " INTO " + clazz.getName();
					if (clazz.isKeyAuto()) {//如果是自增的
						for (Field field : map.keySet()) {
							if (!field.getName().equals(key)) {// 自增的过滤掉主键
								result = map.get(field).invoke(t);
								if(result != null ){
									names += clazz.getFields().get(field)+",";//存储列名
									values += "'" + result + "',";
								}
							}
						}
					} else {
						for (Field field : map.keySet()) {
							result = map.get(field).invoke(t);
							if(result != null ){
								names += clazz.getFields().get(field)+",";//存储列名
								values += "'" + result + "',";
							}
						}
					}
					if(names.length()>0)
						sql += "(" + names.substring(0, names.length()-1) + ") VALUES (" + values.substring(0, values.length()-1) + ")";//########
					break;

				case "UPDATE":
					sql += sqlType + " " + clazz.getName() + " SET ";
					for (Field field : map.keySet()) {
						if (!field.getName().equals(key)) {// 过滤掉主键,最后再拼
							result = map.get(field).invoke(t);
							if (result != null)
								sql += " " + field.getName() + "='" + result + "',";
						}
					}
					sql = sql.substring(0, sql.length() - 1);
					sql += " where " + key + "='" + keyValue + "'";// 拼装主键不考虑int类型的''符号
					break;

				case "DELETE":
					sql += sqlType + " FROM " + clazz.getName() + " WHERE " + key + "='" + keyValue + "'";// 拼装主键不考虑int类型的''符号
					break;

				}
			} else {
				switch (sqlType) {
				case "SELECT":
					sql += sqlType + " * FROM " + clazz.getName()+" WHERE";
					for (Field field : map.keySet()) {
						result = map.get(field).invoke(t);
						if (result != null)
							sql +=  " "+ clazz.getFields().get(field) + "='" + result + "' AND";
					}
					if(sql.contains("="))
						sql = sql.substring(0, sql.length() - 3);
					break;
					
				case "INSERT":
					sql += sqlType + " INTO " + clazz.getName();
					for (Field field : map.keySet()) {
						result = map.get(field).invoke(t);
						if(result != null ){
							names += clazz.getFields().get(field)+",";//存储列名
							values += "'" + result + "',";
						}
					}
					if(names.length()>0)
						sql += "(" + names.substring(0, names.length()-1) + ") VALUES (" + values.substring(0, values.length()-1) + ")";//########
					break;
					
				case "UPDATE":
					sql="";//更新一个实体对应的数据必须使用主键！
					break;
					
				case "DELETE":
					sql += sqlType + " FROM " + clazz.getName();
						for (Field field : map.keySet()) {
							result = map.get(field).invoke(t);
							if(result != null ){
								sql +=  " "+ clazz.getFields().get(field) + "='" + result + "' AND";
							}
						}
					if(sql.contains("="))
						sql = sql.substring(0, sql.length() - 3);
					break;
				}
			}
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}

		return sql;
	}
}
